<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/18 0018
 * Time: 15:59
 */

namespace Admin\Controller;



use Admin\Model\GoodsModel;

class ArticleColumnController extends CommonController
{
    public function index(){

        $db = M('article_column');
        $cate = $this->getCateTree($db,$id=0,$pid='parent_column_id',$zid='column_id');
        $this->assign('list',$cate);


        $this->display();
    }

    public function add(){

        if(IS_POST){
            $data = I('post.');

            $rel = M('article_column')->add($data);
            if($rel){
                die(json_encode(array('state'=>'200','rel'=>'成功！')));
            }

        }

        $db = M('article_column');
        $cate = $this->getCateTree($db,$id=0,$pid='parent_column_id',$zid='column_id');
        $this->assign('cate',$cate);
        $this->display();
    }



    function dels(){
        $role=I("get.id");
        $tableName=I("get.tableName");
        $data['state']=0;
        if($tableName=='article_column'){
            $result=M("article_column")->where("column_id='".$role."'")->save($data);
            if($result!=false){
                $this->success("删除成功！",U('ArticleColumn/index'),3);
            }else{
                $this->error("删除失败！",U("ArticleColumn/index"),3);
            }
        }elseif ($tableName=='article_list'){
            $result=M("article_list")->where("article_id='".$role."'")->save($data);
            if($result!=false){
                $this->success("删除成功！",U('Article/index'),3);
            }else{
                $this->error("删除失败！",U("Article/index"),3);
            }
        }

    }
    function edit(){
        $role=I("get.id");
        $tableName=I("get.tableName");
        if($tableName=='article_column'){
            $result=M("$tableName")->where("column_id='".$role."'")->find();
            $pname = M("$tableName")->where(array('column_id'=>$result['parent_column_id']))
                ->field('column_name as name')->find();
            $result['name'] = $pname['name'];


            $db = M('article_column');
            $cate = $this->getCateTree($db,$id=0,$pid='parent_column_id',$zid='column_id');
            $this->assign('cate',$cate);

            /*foreach($result as $v){
                $column_id=$v['column_id'];
                $parent_column_id=$v['parent_column_id'];
                $column_name=$v['column_name'];
                $state=$v['state'];
                $seo_title=$v['seo_title'];
                $seo_keywords=$v['seo_keywords'];
                $seo_description=$v['seo_description'];

                $this->assign('column_id',$column_id);
                $this->assign('parent_column_id',$parent_column_id);
                $this->assign('column_name',$column_name);
                $this->assign('state',$state);
                $this->assign('seo_title',$seo_title);
                $this->assign('seo_keywords',$seo_keywords);
                $this->assign('seo_description',$seo_description);
            }
            $cate = M('article_column')->where(array('state'=>1))->select();*/

            $this->assign('result',$result);
        }elseif($tableName=='article_list'){
            $result=M("$tableName")->where("article_id='".$role."'")->select();
            foreach($result as $v){
                $article_id=$v['article_id'];
                $column_id=$v['column_id'];
                $article_title=$v['article_title'];
                $article_text=$v['article_text'];
                $release_time=$v['release_time'];
                $amount=$v['amount'];
                $admin_id=$v['admin_id'];
                $seo_title=$v['seo_title'];
                $seo_keywords=$v['seo_keywords'];
                $seo_description=$v['seo_description'];
                $state=$v['state'];

                $this->assign('article_id',$article_id);
                $this->assign('column_id',$column_id);
                $this->assign('article_title',$article_title);
                $this->assign('article_text',$article_text);
                $this->assign('release_time',$release_time);
                $this->assign('amount',$amount);
                $this->assign('admin_id',$admin_id);
                $this->assign('seo_title',$seo_title);
                $this->assign('seo_keywords',$seo_keywords);
                $this->assign('seo_description',$seo_description);
                $this->assign('state',$state);
            }
            $cate = M('article_list')->where(array('state'=>1))->select();
            $this->assign('cate',$cate);


        }
    //    var_dump($role,$tableName);

        $this->display("edit.html$tableName");
    }
    function editarticle_column(){
        $arr=I("post.");

        $id=$arr['save_id'];
        unset($arr['save_id']);

        if($id){
            $result=M("article_column")->where("column_id='".$id."'")->save($arr);
            if($result!=false){
                $this->success("更新成功！",U('ArticleColumn/index'),3);
            }else{
                $this->error("更新失败！",U("ArticleColumn/index"),3);
            }
        }
    }
    function editarticle_list(){
        $arr=I("post.");
        $arr['article_text'] = $_POST['article_text'];
//        var_dump($arr);die;
        $id=$arr['article_id'];
        if($id){
            $result=M("article_list")->where("article_id='".$id."'")->save($arr);
            if($result!=false){
                $this->success("更新成功！",U('Article/index'),3);
            }else{
                $this->error("更新失败！",U("Article/index"),3);
            }
        }
    }

}